package com.hilaver.dzmis.web.servlet.product;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiArmor;
import com.hilaver.dzmis.basicinfo.BiFactory;
import com.hilaver.dzmis.basicinfo.BiFranceCustomer;
import com.hilaver.dzmis.basicinfo.BiYarn;
import com.hilaver.dzmis.product.ProductIdentification;
import com.hilaver.dzmis.product.ProductMeasureSpecificationItem;
import com.hilaver.dzmis.product.ProductYarn;
import com.hilaver.dzmis.service.impl.ProductIdentificationServiceImpl;
import com.hilaver.dzmis.util.StringUtils;
import com.hilaver.dzmis.web.servlet.AbstractBaseServlet;

public class ProductIdentificationServlet extends AbstractBaseServlet {
	
	public final static int ACTION_GET_MAX_REFERENCE_INDEX = 12;
	
	public final static int ACTION_EDIT_MEASURE_SPECIFICATION_ITEM = 13;
	public final static int ACTION_GET_MEASURE_SPECIFICATION_ITEM = 14;
	public final static int ACTION_DELETE_MEASURE_SPECIFICATION_ITEM = 15;
	public final static int ACTION_GET_MEASURE_SPECIFICATION_ITEM_ALL = 16;
	public final static int ACTION_UPDATE_MEASURE_SPECIFICATION = 17;
	public final static int ACTION_GO_BACK = 18;
	
	public final static int ACTION_GET_ALL_PAGINATION_FILTER_IS_NOT_HISTORY = 100;
	public final static int ACTION_SELECT_ALL_PAGINATION_FILTER = 101;
	public final static int ACTION_SEARCH_ALL_PAGINATION_FILTER = 102;
	public final static int ACTION_GET_ALL_PAGINATION_FILTER_IS_HISTORY = 103;
	public final static int ACTION_GET_ALL_PAGINATION_FILTER_SAME_AS = 104;
	
	public final static int ACTION_EDIT_CONFIRMATION = 200;
	
	public final static int ACTION_EDIT_YARN = 201;
	
	public final static int ACTION_GET_ALL_YARN = 202;
	
	public final static int ACTION_GET_YARN = 203;
	
	public final static int ACTION_DELETE_YARN = 204;
	
	public final static int ACTION_GET_VARIANT = 300;
	
	public final static int ACTION_PRODUCT_IN_PROCESS = 400;
	
	private ProductIdentification productIdentification;
	
	private ProductYarn productYarn;
	
	private ProductIdentificationServiceImpl piService;
	
	public ProductIdentificationServlet(){
		super();
		this.simpleName = ProductIdentification.class.getSimpleName();
		this.fullName = ProductIdentification.class.getName();
		piService = new ProductIdentificationServiceImpl();
		piService.setRealFileDir(UPLOAD_TEMP_REAL_DIR);
	}
	
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		try {
			String outputStr = execute(request);
			if (this.action == ACTION_EDIT) {
				response.setContentType(Constants.HTML_RESPONSE_TYPE);
			}
			writer.write(outputStr);
		} catch (Exception e) {
			writer.print(e.getMessage());
			e.printStackTrace();
		}
		writer.close();
	}
	
	protected String execute(HttpServletRequest request) throws Exception {
		super.setAction(request);
		this.piService.setLocale((Locale) request.getSession()
				.getAttribute(Constants.SESSION_LOCALE));
		super.setAcUser(request, this.piService);
		String rtn = "";
		List<String> filterList;
		String ctCollarType = request.getParameter("ctCollarType");
		String ctSeasonNumber = request.getParameter("ctSeasonNumber");
		String reference = request.getParameter("reference");
		switch (this.action) {
		case ACTOIN_GET_ALL_PAGINATION: // "getAllPagination";
			
			break;
		case ACTION_EDIT: // "edit";
			this.fillObj(request);
			rtn = this.piService.edit(this.productIdentification);
			break;
		case ACTION_DELETE: // "delete";
			super.setId(request);
			rtn = this.piService.delete(this.id);
			break;
		case ACTION_GET: // "get one"
			super.setId(request);
			rtn = this.piService.get(this.id);
			break;
		case ACTION_GET_WITH_DESCRIPTION: // "get one with description"
			super.setId(request);
			rtn = this.piService.getWithDescription(this.id);
			break;
		case ACTION_GET_MAX_REFERENCE_INDEX:
			
			rtn = this.piService.getMaxReferenceIndex(ctCollarType, ctSeasonNumber);
			break;
		case ACTION_EDIT_MEASURE_SPECIFICATION_ITEM:
			this.fillMSItem(request);
			rtn = this.piService.editMSItem(this.obj);
			break;
		case ACTION_GET_MEASURE_SPECIFICATION_ITEM:
			super.setId(request);
			rtn = this.piService.getMSItem(this.id);
			break;
		case ACTION_DELETE_MEASURE_SPECIFICATION_ITEM:
			super.setId(request);
			rtn = this.piService.deleteMSItem(this.id);
			break;
		case ACTION_GET_MEASURE_SPECIFICATION_ITEM_ALL:
			super.setId(request);
			rtn = this.piService.getMSItemAll(this.id);
			break;
		case ACTION_UPDATE_MEASURE_SPECIFICATION:
			super.setId(request);
			Integer msId = Integer.parseInt((String)this.getValue(request, "msId"));
			System.out.println(msId);
			rtn = this.piService.updateMS(this.id, msId);
			break;
		case ACTION_GET_ALL_PAGINATION_FILTER: // "getAllPagination filter"
			this.setPaginationParam(request);
			this.setFilterParam(request);
			this.setOtherParameters(request);
			rtn = this.piService.getAllFilterPagination(page, sort, dir, filters);
			break;
		case ACTION_GET_ALL:
			
			break;
		case ACTION_GET_ALL_PAGINATION_FILTER_IS_NOT_HISTORY:
			this.setPaginationParam(request);
			this.setFilterParam(request);
			
			if(this.filters == null){
				this.filters = new String[0];
			}
			filterList = new ArrayList<String>();
			for(int i = 0; i < this.filters.length; i ++){
				filterList.add(this.filters[i]);
			}
			if(ctCollarType != null){
				filterList.add("ctCollarType='" + ctCollarType + "'");
			}
			if(ctSeasonNumber != null){
				filterList.add("ctSeasonNumber='" + ctSeasonNumber + "'");
			}
			if(reference != null){
				filterList.add("reference like '%" + reference + "%'");
			}
			this.filters = filterList.toArray(new String[0]);
			
			rtn = this.piService.getAllIsNotHistoryPagination(page, sort, dir, filters);
			break;
		case ACTION_GET_ALL_PAGINATION_FILTER_IS_HISTORY:
			this.setPaginationParam(request);
			this.setFilterParam(request);
			
			if(this.filters == null){
				this.filters = new String[0];
			}
			filterList = new ArrayList<String>();
			filterList.add("isConfirmed='01'");
			filterList.add("ctDesignType='4'");
			for(int i = 0; i < this.filters.length; i ++){
				filterList.add(this.filters[i]);
			}
			if(ctCollarType != null){
				filterList.add("ctCollarType='" + ctCollarType + "'");
			}
			if(ctSeasonNumber != null){
				filterList.add("ctSeasonNumber='" + ctSeasonNumber + "'");
			}
			if(reference != null){
				filterList.add("reference like '%" + reference + "%'");
			}
			this.filters = filterList.toArray(new String[0]);
			
			
			rtn = this.piService.getAllPagination(page, sort, dir, filters);
			break;
		case ACTION_GET_ALL_PAGINATION_FILTER_SAME_AS:
			this.setPaginationParam(request);
			this.setFilterParam(request);
			
			if(this.filters == null){
				this.filters = new String[0];
			}
			filterList = new ArrayList<String>();
			for(int i = 0; i < this.filters.length; i ++){
				filterList.add(this.filters[i]);
			}
			if(ctCollarType != null){
				filterList.add("ctCollarType='" + ctCollarType + "'");
			}
			if(ctSeasonNumber != null){
				filterList.add("ctSeasonNumber='" + ctSeasonNumber + "'");
			}
			if(reference != null){
				filterList.add("reference like '%" + reference + "%'");
			}
			this.filters = filterList.toArray(new String[0]);
			
			rtn = this.piService.getAllSameAsPagination(sort, dir, filters);
			break;
		case ACTION_SELECT_ALL_PAGINATION_FILTER:
			this.setPaginationParam(request);
			this.setFilterParam(request);
			
			if(this.filters == null){
				this.filters = new String[0];
			}
			filterList = new ArrayList<String>();
			
			for(int i = 0; i < this.filters.length; i ++){
				filterList.add(this.filters[i]);
			}
			if(ctCollarType != null){
				filterList.add("ctCollarType='" + ctCollarType + "'");
			}
			if(ctSeasonNumber != null){
				filterList.add("ctSeasonNumber='" + ctSeasonNumber + "'");
			}
			if(reference != null){
				filterList.add("reference like '%" + reference + "%'");
			}
			this.filters = filterList.toArray(new String[0]);
			
			rtn = this.piService.getAllPagination(page, sort, dir, filters);
			break;
		case ACTION_SEARCH_ALL_PAGINATION_FILTER:
			this.setPaginationParam(request);
			this.setFilterParam(request);
			rtn = this.piService.search(page, sort, dir, filters);
			break;
		case ACTION_EDIT_CONFIRMATION:
			super.fillObj(request);
			this.productIdentification = (ProductIdentification)this.obj;
			rtn = this.piService.editConfirmation(this.productIdentification);
			break;
//		case ACTION_EDIT_YARN:
//			super.setId(request);
//			this.fillProductYarn(request);
//			rtn = this.piService.editProductYarn(this.productYarn);
//			break;
//		case ACTION_GET_ALL_YARN:
//			super.setId(request);
//			rtn = this.piService.getAllYarn(this.id);
//			break;
		case ACTION_GET_YARN:
			super.setId(request);
			rtn = this.piService.getYarn(this.id);
			break;
//		case ACTION_DELETE_YARN:
//			super.setId(request);
//			rtn = this.piService.deleteYarn(this.id);
//			break;
		case ACTION_GET_VARIANT:
			super.setId(request);
			rtn = this.piService.getVariant(this.id);
			break;
		case ACTION_GO_BACK:
			super.setId(request);
			rtn = this.piService.goBack(this.id);
			break;
		case ACTION_PRODUCT_IN_PROCESS:
			this.setFilterParam(request);
			this.setPaginationParam(request);
			rtn = this.piService.getAllInProcessPagination(page, sort, dir, filters);
			break;
		}

		return rtn;
	}
	
	protected void fillObj(HttpServletRequest request) throws Exception {
		super.fillObj(request);
		this.productIdentification = (ProductIdentification)this.obj;
		
		String customerIdStr = (String) getValue(request, "customerId");
		if(!StringUtils.isEmpty(customerIdStr)){
			Integer customerId = Integer.parseInt(customerIdStr);
			BiFranceCustomer customer = new BiFranceCustomer();
			customer.setId(customerId);
			this.productIdentification.setBiFranceCustomer(customer);
		}
		
		String factoryIdStr = (String) getValue(request, "factoryId");
		if(!StringUtils.isEmpty(factoryIdStr)){
			Integer factoryId = Integer.parseInt(factoryIdStr);
			BiFactory factory = new BiFactory();
			factory.setId(factoryId);
			this.productIdentification.setBiFactory(factory);
		}
		
		String frontIdStr = (String) getValue(request, "frontId");
		if(!StringUtils.isEmpty(frontIdStr)){
			Integer frontId = Integer.parseInt(frontIdStr);
			BiArmor front = new BiArmor();
			front.setId(frontId);
			this.productIdentification.setFront(front);
		}
		
		String backIdStr = (String) getValue(request, "backId");
		if(!StringUtils.isEmpty(backIdStr)){
			Integer backId = Integer.parseInt(backIdStr);
			BiArmor back = new BiArmor();
			back.setId(backId);
			this.productIdentification.setBack(back);
		}
		
		String necklineThrumIdStr = (String) getValue(request, "necklineThrumId");
		if(!StringUtils.isEmpty(necklineThrumIdStr)){
			Integer necklineThrumId = Integer.parseInt(necklineThrumIdStr);
			BiArmor necklineThrum = new BiArmor();
			necklineThrum.setId(necklineThrumId);
			this.productIdentification.setNecklineThrum(necklineThrum);
		}
		
		String bottomThrumIdStr = (String) getValue(request, "bottomThrumId");
		if(!StringUtils.isEmpty(bottomThrumIdStr)){
			Integer bottomThrumId = Integer.parseInt(bottomThrumIdStr);
			BiArmor bottomThrum = new BiArmor();
			bottomThrum.setId(bottomThrumId);
			this.productIdentification.setBottomThrum(bottomThrum);
		}
		
		String sleevesThrumIdStr = (String) getValue(request, "sleevesThrumId");
		if(!StringUtils.isEmpty(sleevesThrumIdStr)){
			Integer sleevesThrumId = Integer.parseInt(sleevesThrumIdStr);
			BiArmor sleevesThrum = new BiArmor();
			sleevesThrum.setId(sleevesThrumId);
			this.productIdentification.setSleevesThrum(sleevesThrum);
		}
		
		String sleevesIdStr = (String) getValue(request, "sleevesId");
		if(!StringUtils.isEmpty(sleevesIdStr)){
			Integer sleevesId = Integer.parseInt(sleevesIdStr);
			BiArmor sleeves = new BiArmor();
			sleeves.setId(sleevesId);
			this.productIdentification.setSleeves(sleeves);
		}
	}
	
	protected void fillMSItem(HttpServletRequest request) throws Exception {
		super.fillObj(ProductMeasureSpecificationItem.class.getName(),request);
		ProductMeasureSpecificationItem pmsi = (ProductMeasureSpecificationItem)this.obj;
		
		Integer pId = Integer.parseInt((String) getValue(request, "piId"));
		ProductIdentification pi = new ProductIdentification();
		pi.setId(pId);
		
		pmsi.setProductIdentification(pi);
	}
	
	protected void fillProductYarn(HttpServletRequest request) throws Exception{
		super.fillObj(ProductYarn.class.getName(), request);
		this.productYarn = (ProductYarn)this.obj;
		
		BiYarn yarn = new BiYarn();
		yarn.setId(Integer.parseInt(request.getParameter("biYarnId")));
		this.productYarn.setBiYarn(yarn);
		
		this.productIdentification = new ProductIdentification();
		this.productIdentification.setId(Integer.parseInt(request.getParameter("piId")));
		this.productYarn.setProductIdentification(this.productIdentification);
		
	}
	
	protected void setOtherParameters(HttpServletRequest request) throws Exception{
		String ctCollarType = (String)this.getValue(request, "ctCollarType");
		if(this.filters == null){
			this.filters = new String[0];
		}
		Integer paraLength = this.filters.length + 1;
		String[] tmp = new String[paraLength];
		System.arraycopy(this.filters, 0, tmp, 0, this.filters.length);
		tmp[tmp.length - 1] = "ctCollarType=" + ctCollarType;
		this.filters = tmp;
	}
}
